#pragma once

#include <string>
#include "msgdef.h"

// C runtime header
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

// base header
#include "base/base.h"
#include "serial_port.h"
// duilib
#include "duilib/UIlib.h"
#include "tcp_client.h"

#include "lua_wraper.h"
#include "global.h"
#include <fstream>
#include <istream>

using namespace std;
class TcpClientForm :
	public ui::ChildBox,
	public TcpClientLibevent::TcpClientObserver,
	public LuaBindInterface {
public:
	TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p);
	~TcpClientForm();
	virtual void Init() override;
	TcpClientLibevent* ClientEvent();
	LuaDelegate* LuaVM();

protected:
	virtual void OnConnected();
	virtual void OnDisConnected(std::string) ;
	virtual void OnData(uint8_t* dat, uint64_t len) ;
	virtual void OnClose();
	void ShowDataInEdit(const char*) override;

private:
	ui::Label* m_label_1;
	ui::RichEdit* m_rich_edit_1;
	ui::RichEdit* m_rich_edit_2;
	ui::RichEdit* m_uart_send_edit;
	ui::Button* m_btn_send_data;
	ui::Button* m_btn_save_lua;
	ui::Button* m_btn_close_form;
	ui::CheckBox* m_check_box_1;
	ui::CheckBox* m_check_box_2;
	ui::CheckBox* m_check_box_3;
	ui::CheckBox* m_check_box_4;
	TcpClientLibevent* mClient;
	std::fstream mLuaFileRead;
	std::ofstream mLuaFileEdit;

	virtual void HandleMessage(ui::EventArgs& msg);
	std::string mLuaScript;
	std::string m_url;
	int m_port;
	bool m_connected;
	LuaDelegate* mLua;
};